﻿module ExecutionEvents



open System
open System.ServiceModel
open Features
open Scenarios
open Steps
open Sources
open ParsingEvents



[<ServiceContract(Namespace="http://www.forethought.com/")>]
type public IExecutionTrigger =
   [<OperationContract>]
   abstract member ExecutionStarted : unit -> unit
   
   [<OperationContract>]
   abstract member ExecutionEnded : unit -> unit
   
   [<OperationContract>]
   abstract member ParsingError : error:exn * l:SourceLocation -> unit
   
   [<OperationContract>]
   abstract member FeatureStarted : feature:SectionArgs<FeatureHeader> -> SectionArgs<FeatureHeader>
   
   [<OperationContract>]
   abstract member FeatureEnded : feature:SectionArgs<FeatureHeader> -> SectionArgs<FeatureHeader>
   
   [<OperationContract>]
   abstract member ScenarioStarted : scenario:SectionArgs<ScenarioHeader> -> SectionArgs<ScenarioHeader>
   
   [<OperationContract>]
   abstract member ScenarioEnded : scenario:SectionArgs<ScenarioHeader> -> SectionArgs<ScenarioHeader>
   
   [<OperationContract>]
   abstract member StepStarted : step:SectionArgs<Step> -> SectionArgs<Step>

   [<OperationContract>]
   abstract member StepEnded : step:SectionArgs<Step> -> SectionArgs<Step>



type public ExecutionTrigger() =   
   //inherit MarshalByRefObject()

   let _onExecutionStarted = new Event<_>()
   let _onExecutionEnded = new Event<_>()
   let _onParsingError = new Event<ParsingError>()

   let _onFeatureStarted = new Event<SectionArgs<FeatureHeader>>()
   let _onFeatureEnded = new Event<SectionArgs<FeatureHeader>>()

   let _onScenarioStarted = new Event<SectionArgs<ScenarioHeader>>()
   let _onScenarioEnded = new Event<SectionArgs<ScenarioHeader>>()

   let _onStepStarted = new Event<SectionArgs<Step>>()
   let _onStepEnded = new Event<SectionArgs<Step>>()
   
   [<CLIEvent>]
   member this.OnExecutionStarted = _onExecutionStarted.Publish

   [<CLIEvent>]
   member this.OnExecutionEnded = _onExecutionEnded.Publish

   [<CLIEvent>]
   member this.OnParsingError = _onParsingError.Publish

   [<CLIEvent>]
   member this.OnFeatureStarted = _onFeatureStarted.Publish

   [<CLIEvent>]
   member this.OnFeatureEnded = _onFeatureEnded.Publish

   [<CLIEvent>]
   member this.OnScenarioStarted = _onScenarioStarted.Publish

   [<CLIEvent>]
   member this.OnScenarioEnded = _onScenarioEnded.Publish

   [<CLIEvent>]
   member this.OnStepStarted = _onStepStarted.Publish

   [<CLIEvent>]
   member this.OnStepEnded = _onStepEnded.Publish

   interface IExecutionTrigger with
      member this.ExecutionStarted() = 
         _onExecutionStarted.Trigger()

      member this.ExecutionEnded() = 
         _onExecutionEnded.Trigger()

      member this.ParsingError(error : exn, location : SourceLocation) = 
         _onParsingError.Trigger({Error = error; Location = location})

      member this.FeatureStarted(args : SectionArgs<FeatureHeader>) =          
         _onFeatureStarted.Trigger(args)
         args

      member this.FeatureEnded(args : SectionArgs<FeatureHeader>) =          
         _onFeatureEnded.Trigger(args)
         args

      member this.ScenarioStarted(args : SectionArgs<ScenarioHeader>) = 
         _onScenarioStarted.Trigger(args)
         args

      member this.ScenarioEnded(args : SectionArgs<ScenarioHeader>) = 
         _onScenarioEnded.Trigger(args)
         args

      member this.StepStarted(args : SectionArgs<Step>) = 
         _onStepStarted.Trigger(args)
         args

      member this.StepEnded(args : SectionArgs<Step>) = 
         _onStepEnded.Trigger(args)
         args

